﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace Loong.Admin.Users
{
    public class UserConfiguration : IEntityTypeConfiguration<User>
    {
        public void Configure(EntityTypeBuilder<User> b)
        {
            b.ToTable("User");

            b.ConfigureByConvention();

            b.Property(u => u.UserName)
                .IsRequired()
                .HasMaxLength(UserConsts.MaxUserNameLength);

            b.Property(u => u.NormalizedUserName)
                .IsRequired()
                .HasMaxLength(UserConsts.MaxNormalizedUserNameLength);

            b.Property(u => u.Name).HasMaxLength(UserConsts.MaxNameLength);

            b.Property(u => u.Surname).HasMaxLength(UserConsts.MaxSurnameLength);

            b.Property(u => u.Email)
                .IsRequired()
                .HasMaxLength(UserConsts.MaxEmailLength);

            b.Property(u => u.NormalizedEmail)
                .IsRequired()
                .HasMaxLength(UserConsts.MaxNormalizedEmailLength);

            b.Property(u => u.EmailConfirmed).HasDefaultValue(false);

            b.Property(u => u.PasswordHash).HasMaxLength(UserConsts.MaxPasswordHashLength);

            b.Property(u => u.SecurityStamp)
                .IsRequired()
                .HasMaxLength(UserConsts.MaxSecurityStampLength);

            b.Property(u => u.PhoneNumber).HasMaxLength(UserConsts.MaxPhoneNumberLength);

            b.Property(u => u.PhoneNumberConfirmed).HasDefaultValue(false);

            b.Property(u => u.TwoFactorEnabled).HasDefaultValue(false);

            b.Property(u => u.LockoutEnabled).HasDefaultValue(false);

            b.Property(u => u.IsExternal)
                .IsRequired()
                .HasDefaultValue(false);

            b.HasIndex(u => u.NormalizedUserName);
            b.HasIndex(u => u.NormalizedEmail);
            b.HasIndex(u => u.UserName);
            b.HasIndex(u => u.Email);
        }
    }
}
